# -*- coding: utf-8 -*-

from django.http import JsonResponse
from django.db.models import Q

from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination

from hardware.models import Hardware
from hardware.serializers import HardwareSerializer
from user.models import UserProfile


class HardwarePagination(PageNumberPagination):
    '''自定义分页
    '''
    #默认每页显示的个数
    page_size = 20
    #可以动态改变每页显示的个数
    page_size_query_param = 'page_size'
    #页码参数
    page_query_param = 'page'
    #最多能显示多少页
    max_page_size = 100


class HardwareListView(APIView):
    def get(self, request, format=None):
        q = Q(display="True")
        my_dict = {}
        q1 = Hardware.objects.filter(q).order_by('-date')
        paginate = HardwarePagination()
        page_obj = paginate.paginate_queryset(q1, request)           
        serializer = HardwareSerializer(page_obj, many=True)
        return JsonResponse({"count": q1.count(), "results": serializer.data})
    
    
class HardwareSearchView(APIView):
    def get(self, request):
        name = request.GET.get('name')
        if name:
            name = name.strip()
        category = request.GET.get('category')
        chip = request.GET.get('chip')
        user = request.GET.get('user')
        userobj = UserProfile.objects.filter(Q(username__iexact=user) & Q(status=True)).first()
        q = Q()
        my_dict = {}
        my_dict["group"] = None
        if name:
            q0 = Q(group=None)
            if not userobj or user == 'guest':
                q1 = Q(guest="True")
            else:
                q1 = Q(display="True")
            if userobj and userobj.group:
                q2 = Q(group=userobj.group)
                q3 = Hardware.objects.filter(Q(number__iexact=name) & q2 & (q0 | q1))
            else:
                q3 = Hardware.objects.filter(Q(number__iexact=name) & q0 & q1)
            if q3: 
                serializer = HardwareSerializer(q3, many=True)
                return JsonResponse({"count": q3.count(), "results": serializer.data}) 
            else:    
                my_dict["title__icontains"] = name
        if category:
            my_dict["category"] = category
        if chip:
            my_dict["chip__name"] = chip
        if not userobj or user == 'guest':
            my_dict["guest"] = True
        else:
            my_dict["display"] = True
        for i in my_dict:
            q.add(Q(**{i: my_dict[i]}), Q.AND)
        if userobj and userobj.group:
            q = Hardware.objects.filter(q | Q(group=userobj.group)).order_by('category', 'title', '-date')
        else:
            q = Hardware.objects.filter(q).order_by('category', 'title', '-date')
        paginate = HardwarePagination()
        page_obj = paginate.paginate_queryset(q, request)           
        serializer = HardwareSerializer(page_obj, many=True)
        return JsonResponse({"count": q.count(), "results": serializer.data})
 
 
def show_hardware_num(request):
    user = request.GET.get('user')
    userobj = UserProfile.objects.filter(Q(username__iexact=user) & Q(status=True)).first()
    q0 = Q(group=None)
    if not userobj or user == 'guest':
        q1 = Q(guest=True)
    else:
        q1 = Q(display=True)
    if userobj and userobj.group:
        q2 = Q(group=userobj.group)
        num = Hardware.objects.filter((q0 & q1) | q2).count()
    else:
        num = Hardware.objects.filter(q0 & q1).count()
    return JsonResponse({'num_hardware': num}, safe=False)



